<template>
    <div class="wrapper">
        <header>
            <div class="title">
                <p>商家信息</p>
            </div>
        </header>

        <!-----商家logo----->
        <div class="business-logo">
            <img :src="business.businessImg">
        </div>
        <!-----商家信息----->
        <div class="business-info">
            <h3 id="shop-name">{{ business.businessName }}</h3>
            <p>{{ business.starPrice }}起送 {{ business.deliveryPrice }}配送</p>
            <p>{{ business.businessAddress }}</p>
        </div>

        <!--------菜品详细--------->
        <ul class="business-dishes" v-for="(item, index) in foodArr">
            <li>
                <img :src="item.foodImg">
                <div class="dishes-info-left">
                    <h3>{{ item.foddName }}</h3>
                    <p>{{ item.foodExplain }}</p>
                    <p>价格: {{ item.foodPrice }}元</p>
                    <p class="price" hidden>{{ item.foodPrice }} </p>
                </div>
                <div class="dishes-info-right">
                    <div>
                        <i class="fa fa-minus-circle min" v-show="item.quantity!=0"  @click="minus(index)"></i>
                    </div>
                    <p><span v-show="item.quantity!=0">{{item.quantity}}</span></p>
                    <div>
                        <i class="fa fa-plus-circle pls" @click="add(index)"></i>
                    </div>
                </div>
            </li>
        </ul>

        <ul>
            <li style="height: 20vw;"></li> <!--格式用,否则底部fixed固定最后一li被遮住-->
        </ul>

        <!----底部订单（购物车）样式---->
        <div class="shopcar">
            <div class="shopcar-top">
                <p>满10减1 满45减40</p>
            </div>

            <div class="shopcar-bottom">
                <img id="shopcarimg"  src="https://gw.alicdn.com/imgextra/i4/O1CN01iIDV581uJppBZ0p5Q_!!6000000006017-2-tps-249-249.png">
                <img id="shopcarimg" v-show="totalQuantity!=0" style="position: absolute;" src="https://gw.alicdn.com/imgextra/i2/O1CN01uGE6Bn1m4FQ1OiPfF_!!6000000004900-2-tps-249-249.png">
                <p style="position: absolute; left: 12vw; top: 2vw; background-color: red; width: 4vw; height: 4vw; font-size: 3vw; color: aliceblue; border-radius: 4vw; text-align: center;" v-show="totalQuantity!=0">
                    {{totalQuantity}}
                </p>             <!--小红点-->
                <div class="shopcar-bottom-left">
                    <div class="shopcar-bottom-left-top">
                        <p id="tot-money">￥{{ totalPrice }}</p>
                    </div>
                    <p>配送{{ business.deliveryPrice }}元</p>
                </div>
                <div class="shopcar-bottom-right" v-show="totalPrice<business.deliveryPrice" id="goto_pay" style="background-color: grey;"> <!---不满配送按钮-->
                    {{ business.starPrice }}元起送
                </div>
                <div class="shopcar-bottom-right" v-show="totalPrice>=business.deliveryPrice" id="goto_pay" @click="toOrder"> <!---满足配送按钮-->
                    去支付
                </div>
            </div>
        </div>
    </div>


</template>

<script>
export default {
    name: 'BusinessInfo',
    data() {
        return {
            businessId: this.$route.query.businessId,
            business: {},
            foodArr: [],
            user: {}
        }
    },
    created() {
        this.user = this.$getSessionStorage('user');
        //根据businessId查询商家信息
        this.$axios.post('BusinessController/getBusinessById', this.$qs.stringify({
            businessId: this.businessId
        })).then(response => {
            this.business = response.data;
        }).catch(error => {
            console.error(error);
        });

        //根据businessId查询所属食品信息
        this.$axios.post('FoodController/listFoodByBusinessId', this.$qs.stringify({
            businessId: this.businessId
        })).then(response => {
            this.foodArr = response.data;
            for (let i = 0; i < this.foodArr.length; i++) {
                this.foodArr[i].quantity = 0;
            }

            //如果已登录，那么需要去查询购物车中是否已经选购了某个食品
            if (this.user != null) {
                this.listCart();
            }
        }).catch(error => {
            console.error(error);
        });
    },
    methods: {
        listCart() {
            this.$axios.post('CartController/listCart', this.$qs.stringify({
                businessId: this.businessId,
                userId: this.user.userId
            })).then(response => {
                let cartArr = response.data;
                //遍历所有食品列表
                for (let foodItem of this.foodArr) {
                    foodItem.quantity = 0;
                    for (let cartItem of cartArr) {
                        if (cartItem.foodId == foodItem.foodId) {
                            foodItem.quantity = cartItem.quantity;
                        }
                    }
                }
                this.foodArr.sort();
            }).catch(error => {
                console.error(error);
            });
        },
        add(index) {
            //首先做登录验证
            if (this.user == null) {
                this.$router.push({
                    path: '/login'
                });
                return;
            }
            if (this.foodArr[index].quantity == 0) {
                //做insert
                this.savaCart(index);
                this.foodArr[index].quantity = 1;

            } else {
                //做update
                this.updateCart(index, 1);
            }
        },
        minus(index) {
            if (this.foodArr[index].quantity > 1) {
                //做update
                this.updateCart(index, -1);
            } else {
                //做delete
                this.removeCart(index);
            }
        },
        savaCart(index) {
            this.$axios.post('CartController/saveCart', this.$qs.stringify({
                businessId: this.businessId,
                userId: this.user.userId,
                foodId: this.foodArr[index].foodId
            })).then(response => {
                if (response.data == 1) {
                    //此食品数量要更新为1；
                    this.foodArr[index].quantity = 1;
                    this.foodArr.sort();
                } else {
                    alert('向购物车中添加食品失败！');
                }
            }).catch(error => {
                console.error(error);
            });
        },
        updateCart(index, num) {
            this.$axios.post('CartController/updateCart', this.$qs.stringify({
                businessId: this.businessId,
                userId: this.user.userId,
                foodId: this.foodArr[index].foodId,
                quantity: this.foodArr[index].quantity + num
            })).then(response => {
                if (response.data == 1) {
                    //此食品数量要更新为1或-1；
                    this.foodArr[index].quantity += num;
                    this.foodArr.sort();
                } else {
                    alert('向购物车中更新食品失败！');
                }
            }).catch(error => {
                console.error(error);
            });
        },
        removeCart(index) {
            this.$axios.post('CartController/removeCart', this.$qs.stringify({
                businessId: this.businessId,
                userId: this.user.userId,
                foodId: this.foodArr[index].foodId
            })).then(response => {
                if (response.data == 1) {
                    //此食品数量要更新为0；视图的减号和数量要消失
                    this.foodArr[index].quantity = 0;
                    this.foodArr.sort();
                } else {
                    alert('从购物车中删除食品失败！');
                }
            }).catch(error => {
                console.error(error);
            });
        },
        toOrder() {
            this.$router.push({
                path: '/orders',
                query: {
                    businessId: this.business.businessId
                }
            });
        }
    },
    computed: {
        //食品总价格
        totalPrice() {
            let total = 0;
            for (let item of this.foodArr) {
                total += item.foodPrice * item.quantity;
            }
            return total;
        },
        //食品总数量
        totalQuantity() {
            let quantity = 0;
            for (let item of this.foodArr) {
                quantity += item.quantity;
            }
            return quantity;
        },
        //结算总价格
        totalSettle() {
            return this.totalPrice + this.business.deliveryPrice;
        }
    }
}

</script>

<style scoped>
/***********头部*********/
.wrapper {
    width: 100%;
    height: 100%;
}

.wrapper header {
    width: 100%;
    height: 12vw;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #51b9fe;
}

.wrapper header .title p {
    font-size: 4.5vw;
    color: #fff;
}

/******LOGO********/
.wrapper .business-logo {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 3vw;
    margin-bottom: 3vw;
}

.wrapper .business-logo img {
    width: 40vw;
    height: 40vw;
    border-radius: 5px;
}


/****信息****/
.wrapper .business-info {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.wrapper .business-info h3 {
    font-size: 4vw;
    color: #333;
}

.wrapper .business-info p {
    font-size: 3vw;
    color: rgba(42, 34, 34, 0.579);
    margin-top: 1vw;
}


/******菜品********/
.wrapper .business-dishes {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
}

.wrapper .business-dishes li {
    width: 100%;
    box-sizing: border-box;
    padding: 2.5vw;
    display: flex;
    user-select: none;
    border-bottom: solid 1px #f0f0f0;
    margin-bottom: 0vw;
}

.wrapper .business-dishes li img {
    width: 20vw;
    height: 20vw;
    border-radius: 5px;
    -webkit-transition: width 1s, height 1s, -webkit-transform 1s; /* For Safari 3.1 to 6.0 */
    transition: width 1s, height 1s, transform 1s;
}

.wrapper .business-dishes li img:hover {
    width: 30vw;
    height: 30vw;
    border-radius: 5px;
    -webkit-transform: rotate(360deg); /* Chrome, Safari, Opera */
    transform: rotate(360deg);
}

.wrapper .business-dishes li .dishes-info-left {
    width: 100vw;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    margin-left: 3vw;
}

.wrapper .business-dishes li .dishes-info-right {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 5vw;
}

.wrapper .business-dishes li .dishes-info-left h3 {
    font-size: 3.9vw;
    color: #333;
}

.wrapper .business-dishes li .dishes-info-left p {
    font-size: 2.9vw;
    color: gray;
}

.wrapper .business-dishes li .dishes-info-right i {
    font-size: 5vw;
    color: #51b9fe;
}

.wrapper .business-dishes li .dishes-info-right p {
    margin: 0 1vw 0 1vw;
}


/***********满减信息栏*************/
.wrapper .shopcar .shopcar-top {
    bottom: 13vw;
    left: 0;
    width: 100%;
    height: 5vw;
    background-color: orange;
    position: fixed;
    text-align: center;
}

.wrapper .shopcar .shopcar-top p {
    font-size: 3vw;
    color: #fff;
    line-height: 5vw;
}

.wrapper .shopcar .shopcar-bottom {
    bottom: 0;
    left: 0;
    width: 100%;
    height: 13vw;
    background-color: rgb(255, 255, 255);
    position: fixed;
    display: flex;
    align-items: center;
}

.wrapper .shopcar .shopcar-bottom .shopcar-bottom-left {
    width: 100%;
    flex-direction: column;
    user-select: none;
    cursor: pointer;
}

.wrapper .shopcar .shopcar-bottom img{
    height: 20vw;
    width: 20vw;
    margin-bottom: 5vw;
}

.wrapper .shopcar .shopcar-bottom p {
    font-size: 3vw;
    margin-left: 3vw;
}

.wrapper .shopcar .shopcar-bottom .shopcar-bottom-right {
    background-color: #0097ff;
    color: #fff;
    width: 30%;
    height: 70%;
    font-size: 2.4vw;
    border-radius: 20vw;
    padding: 0 0.6vw;

    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 2vw;
    /*这种div中的文字居中，记住*/
}

.wrapper .shopcar .shopcar-bottom .shopcar-bottom-left .shopcar-bottom-left-top {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    width: 100%;

}

</style>